﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TiPort.TiEntityObjects;
using AppPort;
using TI_EntityLib.TiEntityObjects.DayLog;
using TiEntityObject.Transport;
using System.IO;
using TI;
using TiEntityObject.Notification;

namespace Controllers
{
    public class AdministrationController : Controller 
    {
        private static AdministrationController instance;
        
        public static AdministrationController Instance
        {
            get
            {
                if (AdministrationController.instance == null)
                {
                    AdministrationController.instance = new AdministrationController();
                }
                return AdministrationController.instance;
            }
        }

        #region DayLog
        /// <summary>
        /// Вернуть список дневных логов 
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        public string GetDayLogs(ServiceCmd p)
        {
           var td=GetFtpRootDayCollection();
           List<DayLog> result = new List<DayLog>();
 
           foreach (DateTime d in td)
           {
               DayStatistic ds = new DayStatistic(d);
               DayLog dl = new DayLog(d);
               dl.DayStatistic = ds;
               result.Add(dl);
           }
           string s = Helper.Json<DayLog>.ListToJson(result);
           return Controller.FormatResponceString (true ,s) ;

        }

        private List<DateTime> GetFtpRootDayCollection()
        {
            List<DateTime> r= new List<DateTime> ();
            string root = TransportConfig.Instance.FtpRoot;
            string [] d=Directory.GetDirectories(root);
            DateTime pd;
            string s1 = string.Empty;
            foreach (string s in d)
            {
                if (s.Length < 10)
                    continue;
                s1 = s.Substring(s.Length - 10);
                if(DateTime .TryParse (s1,out pd))
                    r.Add (pd);
            }
            return r;
        }
        #endregion
        
        public string GetDayDocuments(ServiceCmd p) {
            return Controllers.AdministrationControllerCommands.GetDayDocuments.Run(p);          
        }

        /// <summary>
        /// Сформировать HTML на основе документа и вернуть ссылку
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        public string OpenHtmlDocument(ServiceCmd p)
        {
            return Controllers.AdministrationControllerCommands.OpenHtmlDocument.Run (p);
        }

        public string SaveAttachContent(ServiceCmd p)
        {
            return Controllers.AdministrationControllerCommands.SaveAttachContent.Run(p);
        }

        public string UpdateStateDocumentMetaItem(ServiceCmd p)
        {
            return Controllers.AdministrationControllerCommands.UpdateStateDocumentMetaItem.Run(p);
        }

        public string LoadAttachContent(ServiceCmd p)
        {
            return Controllers.AdministrationControllerCommands.LoadAttachContent .Run(p);
        }


        public string PopulateRawRows(ServiceCmd p)
        {
            return Controllers.AdministrationControllerCommands.PopulateRawRows.Run(p);
        }

        public string GetDrugItems(ServiceCmd p)
        {
            return Controllers.AdministrationControllerCommands.GetDrugItems.Run(p);
        }        
        
    }
}
